import App from './App'
import { ENV } from '.env.js'
import { defaultShare, isWeChatBrowser } from "@/common/util";

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false

import {
  isBoj,
  getDicts,
} from './common/util.js'
import uView from "uview-ui";

Vue.use(uView);

import aEmpty from '@/components/aEmpty.vue'
Vue.component('a-empty', aEmpty)

uni.$u.setConfig({
	// 修改$u.config对象的属性
	config: {
		// 修改默认单位为rpx，相当于执行 uni.$u.config.unit = 'rpx'
		// unit: 'rpx'
	},
	props: {
		navbar: {
			leftIconColor: '#000',
			rightIconColor: '#000',
      titleStyle: 'color: #000;',
      autoBack: false
		},
	}
})
Vue.prototype.getDicts = getDicts
Vue.prototype.baseUrl = ENV.BASE_API
Vue.prototype.baseAppId = ENV.BASE_APPID
Vue.prototype.httpUrl = ENV.HTTP_URL


uni.getSystemInfo({
  success: (res) => {
    // 根据 屏幕高度 进行判断
    let screenHeight = res.screenHeight
    let bottom = res.safeArea.bottom
    Vue.prototype.windowHeight = res.windowHeight // 屏幕高度
    Vue.prototype.isFullSucreen = screenHeight !== bottom // 当前设备是否需要适配 true为需要 底部留 30px 距离
    Vue.prototype.statusBarHeight = res.statusBarHeight // 状态栏高度
  }
})

Vue.prototype.navLeftClick = () => {
  const pages = getCurrentPages()
  if(pages.length == 1) {
    uni.reLaunch({
      url: '/pages/index/index'
    })
  } else {
    uni.navigateBack({
      delta: 1,
    })
  }
}

// 跳转bar
Vue.prototype.jumpTab = function(url) {
  uni.switchTab({
    url
  })
}

// 跳转非bar
Vue.prototype.jumpTo = function(url, params) {
  if (isBoj(params)) {
    url += '?'
    for (const key in params) {
      url += `${key}=${params[key]}&`
    }
    url = url.substr(0, url.length - 1)
  }
  uni.navigateTo({
    url
  })
}

// 关闭所有页面
Vue.prototype.reLaunch = function(url, params) {
  if (isBoj(params)) {
    url += '?'
    for (const key in params) {
      url += `${key}=${params[key]}&`
    }
    url = url.substr(0, url.length - 1)
  }
  uni.reLaunch({
    url
  })
}

// 关闭当前页面
Vue.prototype.redirectTo = function(url, params) {
  if (isBoj(params)) {
    url += '?'
    for (const key in params) {
      url += `${key}=${params[key]}&`
    }
    url = url.substr(0, url.length - 1)
  }
  uni.redirectTo({
    url
  })
}

// 返回上一页
Vue.prototype.goBack = function(num = 1) {
  const pages = getCurrentPages()
  if(pages.length == 1) {
    uni.reLaunch({
      url: '/pages/index/index'
    })
  } else {
    uni.navigateBack({
      delta: num,
    })
  }
}

App.mpType = 'app'
const app = new Vue({
  ...App
})

const pathList = [
  '/pages/goodsDetail/goodsDetail',
  '/pages/my/brochureDetail',
  '/pages/my/commonDocumentsDetail',
  '/pages/my/marketingVideoDetail',
  '/pages/my/commonDocumentsDetail',
  '/pages/my/promotionPosterDetail'
]

app.$router.afterEach((to, from) => {
  if(isWeChatBrowser()) {
    if(pathList.findIndex(v => v == from.path) != -1) {
      defaultShare()
    }
  }
});

app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif